package com.hzau.tourist_service_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzau.tourist_service_sys.dao.Hotel.*;
import com.hzau.tourist_service_sys.pojo.Hotel.*;
import com.hzau.tourist_service_sys.result.Result;
import com.hzau.tourist_service_sys.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Transactional

@Service
public class HotelServiceImpl implements HotelService {
    @Autowired
    HotelDao hotelDao;
    @Autowired
    HotelAdminDao hotelAdminDao;
    @Autowired
    HotelOrdersDao hotelOrdersDao;
    @Autowired
    HotelRoomsDao hotelRoomsDao;
    @Autowired
    RoomTypePriceDao roomTypePriceDao;
    @Autowired
    SellDao sellDao;
    @Autowired
    DiscountDao discountDao;
    int pagesize=10;
    /*

    *按照条件查询酒店
    * */
    @Override
    public Result search(Hotel hotel) {
        if(hotel.getPagenum()==null){hotel.setPagenum(0);}
        IPage page=new Page(hotel.getPagenum(),pagesize);
        LambdaQueryWrapper<Hotel> lqw=new LambdaQueryWrapper<>();
        lqw.like(hotel.getHotelName()!=null,Hotel::getHotelName,hotel.getHotelName());
        lqw.ge(hotel.getHotelStar()!=null,Hotel::getHotelStar,hotel.getHotelStar());
        lqw.le(hotel.getMaxStar()!=null,Hotel::getHotelStar,hotel.getMaxStar());
        return Result.success(hotelDao.selectPage(page,lqw).getRecords());
    }
    /*按照酒店Id查找房间,待优化(可能要动数据库)
    * */
    @Override
    public Result infoFind(Hotel hotel) {
//        IPage page=new Page(hotel.getPagenum(),pagesize);
        LambdaQueryWrapper<HotelOrders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(HotelOrders::getHotelId,hotel.getHotelId());
        lqw.ge(HotelOrders::getCheckInTime, LocalDateTime.now());
        lqw.le(HotelOrders::getOrderDays, LocalDate.now());
        List<HotelOrders> hotelOrders = hotelOrdersDao.selectList(lqw);
        LambdaQueryWrapper<HotelRooms> lqw2 =new LambdaQueryWrapper<>();
        List<HotelRooms> hotelRooms = hotelRoomsDao.selectList(lqw2);
        for (HotelRooms hotelRoom : hotelRooms) {
            for (HotelOrders hotelOrder : hotelOrders) {
                hotelRoom.getRoomName().equals(hotelOrder.getRoomName());
                hotelRoom.setPreTag(true);
        }
        }
        return Result.success(hotelRooms);

    }
    /*按照酒店Id查找所有订单
     * */
    @Override
    public Result findAllOrders(Integer hotelId, int pagenum) {
        LambdaQueryWrapper<HotelOrders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(hotelId!=null,HotelOrders::getHotelId,hotelId);
        IPage page=new Page(pagenum,pagesize);
        hotelOrdersDao.selectPage(page, lqw);
        System.out.println(page.getRecords());
        return Result.success(page.getRecords());
    }
    /*按照订单号查详细信息
     * */
    @Override
    public Result findOrderDetails(Integer hr_id, Integer ihotelId) {
        HotelOrders hotelOrders = hotelOrdersDao.selectById(hr_id);
        return Result.success(hotelOrders);
    }
    /*插入新订单*/
    @Override
    public Result insertOrders(HotelOrders hotelOrders) {
        hotelOrders.setCheckInTime(new Timestamp(System.currentTimeMillis()));
        Hotel hotel = hotelDao.selectById(hotelOrders.getHotelId());
        hotel.setRevenue(hotel.getRevenue()+hotelOrders.getTotalConsume());
        hotelDao.updateById(hotel);
        hotelOrdersDao.insert(hotelOrders);
        return Result.success();
    }
/*更新订单信息*/
    @Override
    public Result hotelUpdateOrders(HotelOrders hotelOrders) {
        hotelOrdersDao.updateById(hotelOrders);
        return  Result.success();
    }
/*新增折扣信息*/
    @Override
    public Result insertDiscount(Discount discount) {
        discountDao.insert(discount);
        return Result.success();
    }
/*查看所以房型价格*/
    @Override
    public Result selectAllPrice(long hotelId, int pagenum) {
        IPage<RoomTypePrice> page=new Page(pagenum,pagesize);
        LambdaQueryWrapper<RoomTypePrice> lqw=new LambdaQueryWrapper<>();
        lqw.eq(RoomTypePrice::getHotelId,hotelId);
         roomTypePriceDao.selectPage(page,lqw);
        return Result.success(page);
    }
    /*计算营业额*/
    @Override
    public Result calculateRevenue(long hotelId) {
        float revenue = hotelDao.selectById(hotelId).getRevenue();
        return Result.success(revenue);
    }
    /*计算剩余放假*/
    @Override
    public Result calculateRestRooms(long hotelId) {
        LambdaQueryWrapper<HotelOrders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(HotelOrders::getHotelId,hotelId);
        lqw.ge(HotelOrders::getCheckInTime, LocalDateTime.now());
        lqw.le(HotelOrders::getOrderDays, LocalDate.now());
        List<HotelOrders> hotelOrders = hotelOrdersDao.selectList(lqw);
        LambdaQueryWrapper<HotelRooms> lqw2 =new LambdaQueryWrapper<>();
        List<HotelRooms> hotelRooms = hotelRoomsDao.selectList(lqw2);
        Integer roomnum=0;
        for (HotelRooms hotelRoom : hotelRooms) {
            roomnum++;
            for (HotelOrders hotelOrder : hotelOrders) {
                hotelRoom.getRoomName().equals(hotelOrder.getRoomName());
                hotelRoom.setPreTag(true);
                roomnum--;
            }
        }
        return Result.success(roomnum);
    }
    /*酒店信息*/
    @Override
    public Result hotelInfo(long hotelId) {
        Hotel hotel = hotelDao.selectById(hotelId);
        return Result.success(hotel);
    }
/*促销信息分页*/
    @Override
    public Result selectAllSells(int pagenum) {
        IPage page=new Page(pagenum,pagesize);
        sellDao.selectPage(page, null);
        return Result.success(page.getRecords());
    }
/*促销详情*/
    @Override
    public Result sellDetails(long recommendId) {
        Sell sell = sellDao.selectById(recommendId);
        return Result.success(sell);
    }
/*删除促销信息*/
    @Override
    public Result deleteSells(long recommendId) {
        sellDao.deleteById(recommendId);

        return Result.success();
    }

    @Override
    public Result insertSells(Sell sell) {
        sellDao.insert(sell);
        return Result.success();
    }

    @Override
    public Result hotelFindAllDiscount(long hotelId) {
        LambdaQueryWrapper<RoomTypePrice> lqw=new LambdaQueryWrapper<>();
        lqw.eq(RoomTypePrice::getHotelId,hotelId);
        List<RoomTypePrice> roomTypePrices = roomTypePriceDao.selectList(lqw);
        ArrayList<Long> idlist=new ArrayList<>();
        roomTypePrices.forEach(roomTypePrice -> idlist.add(roomTypePrice.getPriceId()));
        LambdaQueryWrapper<Discount> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Discount::getPriceId,idlist);
        List<Discount> discounts = discountDao.selectList(lambdaQueryWrapper);

        return Result.success(discounts);
    }

    @Override
    public Result deleteDiscount(long hotelId, int roomtype) {
        LambdaQueryWrapper<RoomTypePrice> lqw=new LambdaQueryWrapper<>();
        lqw.eq(RoomTypePrice::getHotelId,hotelId);
        lqw.eq(RoomTypePrice::getRoomType,roomtype);
        List<RoomTypePrice> roomTypePrices = roomTypePriceDao.selectList(lqw);
        ArrayList<Long> idlist=new ArrayList<>();
        roomTypePrices.forEach(roomTypePrice -> idlist.add(roomTypePrice.getPriceId()));
        LambdaQueryWrapper<Discount> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        discountDao.delete(lambdaQueryWrapper);
        return Result.success();
    }

    @Override
    public Result getHotelByAdminId(Integer userId) {
        LambdaQueryWrapper<HotelAdmin> lqw=new LambdaQueryWrapper<>();
        lqw.eq(HotelAdmin::getUserId,userId);
        HotelAdmin hotelAdmin = hotelAdminDao.selectOne(lqw);

        if(hotelAdmin!=null){
        LambdaQueryWrapper<Hotel> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Hotel::getHotelId,hotelAdmin.getHotelId());
        Hotel hotel = hotelDao.selectOne(lambdaQueryWrapper);
        return Result.success(hotel);}
        return new Result(306,"there is no such hotel",null);
    }
//
//    @Override
//    public Result findPre(int pagenum) {
//        return null;
//    }


}
